<template>
  <div class="container">

    <a-tabs default-active-key="1">
      <a-tab-pane key="1" tab="基础设置">
        <IForm :formFrame="frameBase" class="form-container" :formData.sync="form"></IForm>
      </a-tab-pane>
      <a-tab-pane key="2" tab="下方推荐页" force-render>
        <IPage v-model="form.page"></IPage>
        <!-- <IForm :formFrame="frameUI" class="form-container" :formData.sync="form"></IForm> -->
      </a-tab-pane>
    </a-tabs>

    <!-- <IForm :formFrame="frame" class="form-container" :formData.sync="form"></IForm> -->
    <div slot="action" style="display: flex; justify-content: flex-end;">
      <a-button class="btn" @click="cancel">取消</a-button>
      <a-button class="btn" type="primary" @click="submit">提交</a-button>
    </div>
    <!-- <AttrEditor :form.sync="form">
			<div slot="action">
				<a-button class="btn" @click="cancel">取消</a-button>
				<a-button class="btn" type="primary" @click="submit">提交</a-button>
			</div>
		</AttrEditor> -->

  </div>
</template>

<script>
  // import AttrEditor from "./components/AttrEditor"
  import IForm from "@/components/IForm"
  import IPage from "@/components/IPage"

  const resourceType = 'zhili/activities'

  export default {
    components: {
      IForm,
      IPage
      // AttrEditor
    },
    data() {
      return {
        form: {
          page: {}
        },
        // type: 'lottery_egg',
        categoryList: []
      }
    },
    props: {
      uuid: {
        type: String
      },
    },
    computed: {
      categoryOptions() {
        return this.categoryList.map(item => {
          return {
            title: item.title,
            value: item.id
          }
        })
      },
      frameBase() {
        return [{
            title: '活动标题',
            key: 'title',
            newline: true,
            required: true
          },
          // {
          //   title: '副标题',
          //   key: 'sub_title',
          //   placeholder: '例如: 直播中随机抽取10位幸运儿',
          //   newline: true,
          //   // required: true
          // },
          // {
          //   title: '参与码',
          //   key: 'join_codes',
          //   tips: '选填。参与码可设置多个，可在公众号或微信群中发放给用户',
          //   type: 'tags',
          //   newline: true,
          // },
          {
            title: '封面',
            key: 'thumb',
            type: 'image',
            tips: '800*800',
            isShowImageTips: false,
            newline: true,
            sizeLimit: 200,
            required: true
          },
          {
            title: '标签',
            key: 'tags',
            type: 'tags',
            tips:  '在列表标题下方显示，如“热卖新品”等短语。 填写多个仅显示第一个',
            newline: true,
            // required: true,
          },
          // {
          //   title: '轮播图',
          //   key: 'images',
          //   type: 'images',
          //   tips: '560*210',
          //   isShowImageTips: false,
          //   newline: true,
          //   required: true
          // },
          {
            title: '发起价',
            key: 'score_price',
            type: 'number',
            newline: true,
            suffix: '积分',
            tips: '此价格为用户每发起一次助力活动需支付的积分价，不填则为免费发起'
          },
          {
            title: '助力数量',
            key: 'need_user_total',
            type: 'number',
            newline: true,
            // prefix: '',
            // suffix: '小时内集齐助力才可获得奖品',
            tips: '用户需要集够的用户助力数量',
            required: true
          },
          {
            title: '限制时间',
            key: 'time_limit',
            type: 'number',
            newline: true,
            prefix: '发起后需在',
            suffix: '小时内集齐助力才可获得奖品',
            tips: '',
            required: true
          },
          {
            title: '奖品',
            // key: 'need_user_total',
            type: 'text',
            // newline: true,
            text: '创建后返回活动列表中编辑奖品',
            // prefix: '',
            // suffix: '小时内集齐助力才可获得奖品',
            // tips: '用户需要集够的用户助力数量',
            // required: true
          },
          {
            type: 'blank',
          },
          {
            title: '发起人群限制',
            key: 'launch_user_group_id',
            type: 'user_group_id',
            newline: true,
            // suffix: '积分',
            tips: '不选则所有人可发起助力'
          },
          {
            title: '发起次数限制',
            key: 'launch_perday_limit',
            type: 'number',
            prefix: '单用户每天可发起',
            suffix: '次本助力活动',
            newline: true,
            // suffix: '积分',
            tips: '0为不填则为不限制'
          },
          {
            type: 'blank',
          },
          {
            title: '助力人群限制',
            key: 'support_user_group_id',
            type: 'user_group_id',
            newline: true,
            // suffix: '积分',
            tips: '不选则所有用户均可为别人助力'
          },
          {
            title: '助力次数限制',
            key: 'support_perday_limit',
            type: 'number',
            prefix: '单用户在本活动中每天可为',
            suffix: '人助力',
            newline: true,
            // suffix: '积分',
            tips: '0为不填则为不限制'
          },
          {
            type: 'blank',
          },
        ]
      },
      frameUI () {
        return [
          {
            title: '',
            key: 'page',
            type: 'page',
          },
        ]
      }
    },
    created() {},
    mounted() {
      this.uuid = this.uuid || this.$route.params.uuid

      // this.$http.get('/article/categories').then(res => {
      // 	this.categoryList = res.data.list
      // })

      this.initForm()
    },
    watch: {
      uuid() {
        this.initForm()
      }
    },
    methods: {
      initForm() {
        if (this.uuid) {
          this.$http(`/${resourceType}/${this.uuid}`).then(res => {
            this.form = res.data.info
          })
        } else {
          this.form = {
            ...this.$initForm(this.frameBase),
            // ...this.$initForm(this.frameUI)
          }
        }
      },
      cancel() {
        this.uuid = ''
        this.$emit('cancel')
        // this.$router.back(-1)
      },
      create() {
        this.$http.post(`/${resourceType}`, {
          type: this.type,
          ...this.form
        }).then(res => {
          this.$message.success('创建成功')
          this.$emit('success')
          // this.$router.push({
          // 	path: './list'
          // })
        })
      },
      update() {
        this.$http.patch(`/${resourceType}/${this.uuid}`, {
          type: 'update',
          attributes: this.form
        }).then(res => {
          this.$message.success('更新成功')
          this.$emit('success')
          // this.uuid = ''
          // this.form = {}

          // this.$router.push({
          // 	path: '../list'
          // })
        })
      },
      submit() {
        if (this.uuid)
          this.update()
        else
          this.create()
      }
    }
  }
</script>

<style lang="scss" scoped>
  .form-container {
    padding-bottom: 120px;
  }

  .container {
    .btn {
      margin-right: 30px;
    }
  }
</style>
